package com.ruoyi.sbrk.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.sbrk.domain.SbglSbrkRkmx;
import com.ruoyi.sbrk.mapper.SbglSbrkMapper;
import com.ruoyi.sbrk.domain.SbglSbrk;
import com.ruoyi.sbrk.service.ISbglSbrkService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.util.CollectionUtils;

/**
 * 设备入库Service业务层处理
 *
 * @author huacai
 * @date 2024-09-11
 */
@Service
public class SbglSbrkServiceImpl implements ISbglSbrkService
{
    @Autowired
    private SbglSbrkMapper sbglSbrkMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    /**
     * 查询设备入库
     *
     * @param sbrkId 设备入库主键
     * @return 设备入库
     */
    @Override
    public SbglSbrk selectSbglSbrkBySbrkId(String sbrkId)
    {
        return sbglSbrkMapper.selectSbglSbrkBySbrkId(sbrkId);
    }

    /**
     * 查询设备入库列表
     *
     * @param sbglSbrk 设备入库
     * @return 设备入库
     */
    @Override
    public List<SbglSbrk> selectSbglSbrkList(SbglSbrk sbglSbrk)
    {
        return sbglSbrkMapper.selectSbglSbrkList(sbglSbrk);
    }

    /**
     * 新增设备入库
     *
     * @param sbglSbrk 设备入库
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSbglSbrk(SbglSbrk sbglSbrk)
    {
        sbglSbrk.setCreateTime(DateUtils.getNowDate());
        int rows = sbglSbrkMapper.insertSbglSbrk(sbglSbrk);
        insertSbglSbrkRkmx(sbglSbrk);
        return rows;
    }

    /**
     * 批量新增设备入库
     *
     * @param sbglSbrks 设备入库List
     * @return 结果
     */
    @Override
    public int batchInsertSbglSbrk(List<SbglSbrk> sbglSbrks)
    {
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        int count = 0;
        if (!CollectionUtils.isEmpty(sbglSbrks)) {
            try {
                for (int i = 0; i < sbglSbrks.size(); i++) {
                    int row = sbglSbrkMapper.insertSbglSbrk(sbglSbrks.get(i));
                    // 防止内存溢出，每100次提交一次,并清除缓存
                    boolean bool = (i >0 && i%100 == 0) || i == sbglSbrks.size() - 1;
                    if (bool){
                        sqlSession.commit();
                        sqlSession.clearCache();
                    }
                    count = i + 1;
                }
            }catch (Exception e){
                e.printStackTrace();
                // 没有提交的数据可以回滚
                sqlSession.rollback();
            }finally {
                sqlSession.close();
                return count;
            }
        }
        return count;
    }

    /**
     * 修改设备入库
     *
     * @param sbglSbrk 设备入库
     * @return 结果
     */
    @Transactional
    @Override
    public int updateSbglSbrk(SbglSbrk sbglSbrk)
    {
        sbglSbrk.setUpdateTime(DateUtils.getNowDate());
        sbglSbrkMapper.deleteSbglSbrkRkmxBySbrkId(sbglSbrk.getSbrkId());
        insertSbglSbrkRkmx(sbglSbrk);
        return sbglSbrkMapper.updateSbglSbrk(sbglSbrk);
    }

    /**
     * 批量删除设备入库
     *
     * @param sbrkIds 需要删除的设备入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSbglSbrkBySbrkIds(String[] sbrkIds)
    {
        sbglSbrkMapper.deleteSbglSbrkRkmxBySbrkIds(sbrkIds);
        return sbglSbrkMapper.deleteSbglSbrkBySbrkIds(sbrkIds);
    }

    /**
     * 删除设备入库信息
     *
     * @param sbrkId 设备入库主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteSbglSbrkBySbrkId(String sbrkId)
    {
        sbglSbrkMapper.deleteSbglSbrkRkmxBySbrkId(sbrkId);
        return sbglSbrkMapper.deleteSbglSbrkBySbrkId(sbrkId);
    }

    /**
     * 新增设备入库入库明细信息
     *
     * @param sbglSbrk 设备入库对象
     */
    public void insertSbglSbrkRkmx(SbglSbrk sbglSbrk)
    {
        List<SbglSbrkRkmx> sbglSbrkRkmxList = sbglSbrk.getSbglSbrkRkmxList();
        String sbrkId = sbglSbrk.getSbrkId();
        if (StringUtils.isNotNull(sbglSbrkRkmxList))
        {
            List<SbglSbrkRkmx> list = new ArrayList<SbglSbrkRkmx>();
            for (SbglSbrkRkmx sbglSbrkRkmx : sbglSbrkRkmxList)
            {
                sbglSbrkRkmx.setSbrkId(sbrkId);
                System.out.println(sbglSbrkRkmx);
                list.add(sbglSbrkRkmx);
            }
            if (list.size() > 0)
            {
                sbglSbrkMapper.batchSbglSbrkRkmx(list);
            }
        }
    }
}
